package com.fqdf.framework.android.net.response;


import com.fqdf.framework.android.util.CheckLogicUtil;

public class NetBaseWrapper extends BaseRespBean {

    // 用来判断本地异常
    private boolean localException = false;
    // 本地异常内容
    private String localMsg;
    // 返回的code代码
    private int code;
    // 返回的错误描述
    private String desc;

    // 额外扩展字段
    private Object extraObj;

    @Override
    public String toString() {
        return "NetBaseWrapper{" +
                "localException=" + localException +
                ", localMsg='" + localMsg + '\'' +
                ", code=" + code +
                ", desc='" + desc + '\'' +
                ", extraObj=" + extraObj +
                '}';
    }

    private static final String DEFAULT_ERROR_MSG = "服务器离家出走了";

    public NetBaseWrapper() {

    }

    public NetBaseWrapper(boolean exception, String msg) {
        localException = exception;
        localMsg = msg;
    }

    /**
     * 是否出现本地异常
     *
     * @return
     */
    public boolean isLocalException() {
        return localException;
    }

    public void setLocalException(boolean localException) {
        this.localException = localException;
    }

    public void setLocalExceptionMsg(String msg) {
        localMsg = msg;
    }


    public String getLocalMsg() {
        return localMsg;
    }

    public void setLocalMsg(String localMsg) {
        this.localMsg = localMsg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * 获取本地异常内容
     *
     * @return
     */
    public String getLocalExceptionMsg() {
        return localMsg;
    }

    /**
     * 是否网络请求成功
     *
     * @return
     */
    public boolean isNetSuccess() {
        return NetConstants.SERVER_NO_EXCEPTION_CODE == getCode();
    }

    /**
     * 判断是否调用成功，这个调用包括网络错误和本地异常
     *
     * @return
     */
    public boolean isAllSuccess() {

        return isNetSuccess() && !isLocalException();
    }

    /**
     * 返回调用出现异常的时候的错误信息，这个错误信息包括本地异常和网络请求错误2种情况
     *
     * @return
     */

    public String getAllErrMSg() {
        String exceptionMsg = null;

        if (isLocalException()) {
            exceptionMsg = getLocalExceptionMsg();
        } else if (!isNetSuccess()) {
            exceptionMsg = getDesc();
        }

        if (CheckLogicUtil.isEmpty(exceptionMsg)) {
            exceptionMsg = DEFAULT_ERROR_MSG;
        }
        return exceptionMsg;
    }

    public Object getExtraObj() {
        return extraObj;
    }

    public void setExtraObj(Object extraObj) {
        this.extraObj = extraObj;
    }

    /**
     * 判断是否是Authcode认证失败
     *
     * @return
     */
    public boolean isAuthcodeError() {
        return NetConstants.TOKEN_FAIL == getCode();
    }
}
